'use strict';

const Service = require('egg').Service;

class AticalService extends Service {
    //添加文章
    async add(title,con,type,username) {
        await this.app.mysql.query('insert into artical (title,con,type,username) values (?,?,?,?)',[title,con,type,username])
    }
    //修改文章
    async update(title,con,type,id){
        await this.app.mysql.query('update artical set title=?,con=?,type=? where id=?',[title,con,type,id])
    }
    //删除文章
    async del(id){
        await this.app.mysql.query('delete from artical where id=?',[id])
    }
    //查询文章
    async articalList(startIndex,limit,key){
        return await this.app.mysql.query(`select * from artical where title like '%${key}%' limit ${startIndex},${limit}`)
    }
    //查询综述
    async total(key){
        return await this.app.mysql.query(`select count(*) from artical where title like '%${key}%'`)
    }
    //按类目查询文章
    async articalTypeList(type){
        let sql = type === '' ? 'select * from artical':'select * from artical where type=?'

        return await this.app.mysql.query(sql,[type])
    }
    //按作者查询
    async articalUsername(username){
        let sql = username === '' ? 'select * from artical':'select * from artical where username=?'

        return await this.app.mysql.query(sql,[username])
    }

}

module.exports = AticalService;
